package com.shuzi.MediaShare.utils;

import java.math.BigDecimal;

public class StringUtils
{

	/**
	 * 效率不高的获取一个数是2的n次方
	 * 
	 * @param x
	 * @return
	 */
	public static int getTwoCount(int x)
	{

		return (int) (Math.log(x) / Math.log(2));
	}

	public static int GB = 1024 * 1024 * 1024;// 定义GB的计算常量

	public static int MB = 1024 * 1024;// 定义MB的计算常量

	public static int KB = 1024;// 定义KB的计算常量

	/**
	 * 格式化字节数
	 * 
	 * @param bytes
	 * @return
	 */
	public static String bytes2kb(long bytes)
	{
		BigDecimal filesize = new BigDecimal(bytes);
		BigDecimal megabyte = new BigDecimal(MB);
		float returnValue = filesize.divide(megabyte, 2, BigDecimal.ROUND_UP)
				.floatValue();
		if (returnValue > 1)
			return (returnValue + " M");
		BigDecimal kilobyte = new BigDecimal(1024);
		returnValue = filesize.divide(kilobyte, 2, BigDecimal.ROUND_UP)
				.floatValue();
		return (returnValue + " KB");
	}

}
